const app = getApp()
const WXAPI = require('../../wxapi/main')

Page({
  data: {
    finpay: {},
    yunPrice: 0,
    allGoodsAndYunPrice: 0,
    goodsJsonStr: "",
    pingtuanOpenId: undefined, //拼团的话记录团号
    orderType: "", //订单类型，购物车下单或立即支付下单，默认是购物车，
    hasNoCoupons: true,
    coupons: [],
    curCoupon: null, // 当前选择使用的优惠券
    allowSelfCollection: '0', // 是否允许到店自提
    peisongType: 'kd' // 配送方式 kd,zq 分别表示快递/到店自取
  },
  onShow: function () {
    const that = this;

    let postdata = {};
    let orderList = [];
    //立即购买下单
    if ("buyNow" == that.data.orderType) {
      var buyNowInfo = wx.getStorageSync('buyNowInfo');
      orderList.push(buyNowInfo);
    } else {
      //购物车下单
      orderList = wx.getStorageSync('carInfo');
    }

    postdata.orderList = JSON.stringify(orderList);
    WXAPI.preorder(postdata).then(function (res) {
      if (res.type != 'success') {
        wx.showModal({
          title: '错误',
          content: res.message,
          showCancel: false
        })
        return;
      }
      let finpay = res.resultData.row;
      that.setData({
        finpay: finpay
      });
      that.initShippingAddress();
    }) 
  },

  onLoad: function (e) {
    let _data = {
    }
    if (e.orderType) {
      _data.orderType = e.orderType
    }
    this.setData(_data);
  },


  createOrder: function (e) {
    var that = this;
    var finpay = this.data.finpay;
     
     
    let postdata = { remark:""}; // 备注信息
    if (e) {
      postdata.remark = e.detail.value.remark; // 备注信息
    }
    if (!that.data.curAddressData) {
      wx.hideLoading();
      wx.showModal({
        title: '错误',
        content: '请先设置您的收货地址！',
        showCancel: false
      })
      return;
    }
    postdata.sheng = that.data.curAddressData.sheng;
    postdata.shi = that.data.curAddressData.shi;
    postdata.xian = that.data.curAddressData.xian;
    postdata.sjrname = that.data.curAddressData.linkman;
    postdata.sjphone = that.data.curAddressData.mobile;
    postdata.orderList = JSON.stringify(finpay.orderList);
    postdata.peisong = that.data.peisongType;

    for (var i = 0; i < finpay.orderList.length;i++){
      var od = finpay.orderList[i];
      if (od.yuyue == -1){
        wx.showModal({
          title: '库存不足',
          content: od.yanse +'-'+ od.chima +"库存不足",
          showCancel: false
        })
        return;
       }
    }

    WXAPI.orderCreate(postdata).then(function (res) {
      if (res.type != 'success') {
        wx.showModal({
          title: '错误',
          content: res.message,
          showCancel: false
        })
        return;
      }    
      // 下单成功，跳转到订单管理界面
      wx.redirectTo({
        url: "/pages/order-list/index"
      });
    })
  },
  initShippingAddress: function () {
    var that = this;
    WXAPI.defaultAddress(wx.getStorageSync('token')).then(function (res) {
      if (res.type == 'success') {
        var rows = res.resultData.rows;
        var d = null;
        for (let i = 0; i < rows.length; i++) {
          let carShopBean = rows[i];
          if (carShopBean.ismoren == 1){
            d = carShopBean;
          }
        }
        that.setData({
          curAddressData: d
        });
      } else {
        that.setData({
          curAddressData: null
        });
      }
      that.processYunfei();
    })
  },
  processYunfei: function () {
  
  },
  addAddress: function () {
    wx.navigateTo({
      url: "/pages/address-add/index"
    })
  },
  selectAddress: function () {
    wx.navigateTo({
      url: "/pages/select-address/index"
    })
  },
  getMyCoupons: function () {
    var that = this;
    WXAPI.myCoupons({
      token: wx.getStorageSync('token'),
      status: 0
    }).then(function (res) {
      if (res.code == 0) {
        var coupons = res.data.filter(entity => {
          return entity.moneyHreshold <= that.data.allGoodsAndYunPrice;
        });
        if (coupons.length > 0) {
          that.setData({
            hasNoCoupons: false,
            coupons: coupons
          });
        }
      }
    })
  },
  bindChangeCoupon: function (e) {
    const selIndex = e.detail.value[0] - 1;
    if (selIndex == -1) {
      this.setData({
        youhuijine: 0,
        curCoupon: null
      });
      return;
    }
    //console.log("selIndex:" + selIndex);
    this.setData({
      youhuijine: this.data.coupons[selIndex].money,
      curCoupon: this.data.coupons[selIndex]
    });
  },
  radioChange (e) {
    this.setData({
      peisongType: e.detail.value
    })
  }
})